package com.shopping.mall.core.module.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.auth.VO.EnterpriseVo;
import com.shopping.mall.auth.VO.UserVo;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.user.entity.User;
import com.shopping.mall.core.module.user.service.UserExcelService;
import com.shopping.mall.core.module.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.Email;
import java.util.List;
import java.util.Map;

@Api(tags = "用户")
@RestController
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserExcelService userExcelService;


    @PostMapping("/batchAddUser")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NORMAL_ADMIN')")
    public Result<Object> saveSubject(MultipartFile file) {
        userExcelService.saveSubject(file);
        return Result.OK();
    }


    @ApiOperation("发送注册验证码")
    @UnLogin
    @GetMapping("/sendRegisterMail")
    public Result<Object> registerSendMail(@Email @RequestParam(name = "mailToAddress") String mailToAddress) {
        return userService.registerSendMail(mailToAddress);
    }

    @ApiOperation("注册")
    @UnLogin
    @PostMapping("/register")
    public Result<Object> register(@Validated(UserVo.register.class) @RequestBody UserVo userVo) {
        return userService.register(userVo);
    }

    @ApiOperation("登录")
    @UnLogin
    @PostMapping("/login")
    public Result<Object> login(@Validated(UserVo.login.class) @RequestBody UserVo userVo) {
        return userService.login(userVo);
    }

    @ApiOperation("个人信息")
    @GetMapping("/userInfo")
    public Result<Object> userInfo() {
        return userService.userInfo();
    }

    @ApiOperation("退出登录")
    @DeleteMapping("/logout")
    public Result<Object> logout() {
        return userService.logout();
    }

    @ApiOperation("刷新token")
    @UnLogin
    @PostMapping("/refreshToken")
    public Result<Object> refreshToken(@RequestBody Map<String, String> refreshTokenMap) {
        return userService.refreshToken(refreshTokenMap.get("refreshToken"));
    }

    @ApiOperation("修改密码")
    @PutMapping("/changePassword")
    public Result<Object> changePassword(@Validated(UserVo.changePassword.class) @RequestBody UserVo userVo) {
        return userService.changePassword(userVo);
    }

    @ApiOperation("修改个人信息")
    @PutMapping("/changeUserInfo")
    public Result<Object> changeUserInfo(@Validated(UserVo.changeUserInfo.class) @RequestBody UserVo userVo) {
        return userService.changeUserInfo(userVo);
    }

    @ApiOperation("发送忘记密码验证码")
    @GetMapping("/sendForgetMail")
    @UnLogin
    public Result<Object> sendForgetMail(@RequestParam String mailToAddress) {
        return userService.sendForgetMail(mailToAddress);
    }

    @ApiOperation("发送换绑时解除邮箱绑定验证码")
    @GetMapping("/sendUnbindMail")
    public Result<Object> sendUnbindMail() {
        return userService.sendUnbindMail();
    }

    @ApiOperation("发送绑定新邮箱验证码")
    @GetMapping("/sendBindMail")
    public Result<Object> sendBindMail(@RequestParam @Email String newMail) {
        return userService.sendBindMail(newMail);
    }

    @ApiOperation("邮箱换绑")
    @PutMapping("/changeBindMail")
    public Result<Object> changeBindMail(@RequestParam @Email String newMail,
                                         @RequestParam @Length(min = 6, max = 6, message = "验证码必须为6位") String unBindCode,
                                         @RequestParam @Length(min = 6, max = 6, message = "验证码必须为6位") String bindCode) {
        return userService.changeBindMail(newMail, unBindCode, bindCode);
    }

    @UnLogin
    @ApiOperation("/重置密码")
    @PutMapping("/resetPassword")
    public Result<Object> resetPassword(@Validated(UserVo.resetPassword.class) @RequestBody UserVo userVo) {
        return userService.resetPassword(userVo);
    }

    @ApiOperation("/申请成为企业用户")
    @PostMapping("/applyEnterpriseUser")
    public Result<Object> applyEnterpriseUser(@Validated(EnterpriseVo.apply.class) @RequestBody EnterpriseVo enterpriseVo) {
        return userService.applyEnterpriseUser(enterpriseVo);
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<User>> getUserPage(
            @RequestParam(name = "current", defaultValue = "1") Integer current,
            @RequestParam(name = "size", defaultValue = "10") Integer size,
            User user) {
        IPage<User> userPage = userService.getUserPage(current, size, user);
        return Result.OK(userPage);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result<User> update(@Validated(User.update.class) @RequestBody User user) {
        userService.updateById(user);
        return Result.OK();
    }

    @Autowired
    private PasswordEncoder passwordEncoder;
    @ApiOperation("新增")
    @PostMapping
    public Result<Object> add(@Validated(User.insert.class) @RequestBody User user) {
        if (user.getPassword()!=null){
            user.setPassword(passwordEncoder.encode(user.getPassword()));
        }else {
            user.setPassword("$2a$10$rS5OuLEUZ.2vhOXJTsDj2uX9QFcepFQ6VWP0N3pTYlR7HCcKJLBGG");
        }
        userService.save(user);
        return Result.OK();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<User> getById(@RequestParam String id) {
        User user = userService.getById(id);
        return Result.OK(user);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<User>> getAll(User user) {
        List<User> userList = userService.getAll(user);
        return Result.OK(userList);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id) {
        return userService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    @Deprecated
    public Result<Object> batchDelete(@RequestParam List<String> ids) {
        return userService.batchDelete(ids);
    }
}
